###################################################################
### Natural Hazards, Social Policy, and Electoral Performance: ####
### Evidence from the 2017 Earthquake in Mexico City           ####             
### Figures and tables in the main text                        ####                  
### This version: February 16, 2023                            ####                   
###################################################################

#---------------------------------------------------#
# Preliminaries                                     #
#---------------------------------------------------#

rm(list = ls())

#List of packages for session
.packages = c("tidyverse", "stringi", "stringr",
              "estimatr", "patchwork", "modelsummary",
              "spdep", "sf", "MatchIt", "texreg", "sensemakr")

#Install CRAN packages (if not already installed)
.inst = .packages %in% installed.packages()
if(length(.packages[!.inst]) > 0) install.packages(.packages[!.inst])

#Loading packages into session 
lapply(.packages, require, character.only=TRUE)

# Set Working Directory to wherever files are downloaded
#setwd("")

#---------------------------------------------------#
# Loading Data                                      #
#---------------------------------------------------#

#Main datasets for regression analysis and figures

##Master dataset
master <- read.csv("master_cdmx.csv") %>%
  dplyr::select(-1)

mexico_city_map <- st_read(dsn = "master_cdmx_map.shp") %>%
  left_join(master, by = "CLAVEGEO") %>%
  na.omit()

##############################################
### Supplementary Information -- Table S1 ####               
##############################################

#Table S1: Results for models using distance-based measures of damage—PRD candidate for governor

#Distance-based measurements

mod1 = lm_robust(prd_change_gov_12_18~distance_damaged_houses+as.factor(zona.geotec)+urbage+literacy_15+unemployment+seguro_popular+president2018_share_morena+as.factor(name.del), data = master, se_type = "stata")

mod2 = lm_robust(prd_change_gov_12_18~distance_damaged_houses_highrisk+as.factor(zona.geotec)+urbage+literacy_15+unemployment+seguro_popular+president2018_share_morena+as.factor(name.del), data = master, se_type = "stata")

mod3 = lm_robust(prd_change_gov_12_18~distance_damaged_multifamily+as.factor(zona.geotec)+urbage+literacy_15+unemployment+seguro_popular+president2018_share_morena+as.factor(name.del), data = master, se_type = "stata")

texreg::texreg(list(mod1, mod2, mod3), digits = 3)

##############################################
### Supplementary Information -- Table S2 ####               
##############################################

#Table S2: Results for models using per capita measures of damage—PRD candidate for governor

#Per capita measurements

mod1b = lm_robust(prd_change_gov_12_18~damaged_houses_1k+as.factor(zona.geotec)+urbage+literacy_15+unemployment+seguro_popular+president2018_share_morena+as.factor(name.del), data = master, se_type = "stata")

mod2b = lm_robust(prd_change_gov_12_18~damaged_houses_highrisk_1k+as.factor(zona.geotec)+urbage+literacy_15+unemployment+seguro_popular+president2018_share_morena+as.factor(name.del), data = master, se_type = "stata")

mod3b = lm_robust(prd_change_gov_12_18~damaged_multifamily_1k+as.factor(zona.geotec)+urbage+literacy_15+unemployment+seguro_popular+president2018_share_morena+as.factor(name.del), data = master, se_type = "stata")

texreg::texreg(list(mod1b, mod2b, mod3b), digits = 3)

##############################################
### Supplementary Information -- Table S3 ####               
##############################################

#Table S3: Results for models using distance-based measures of damage—PRD candidates for mayor

#PRD distance based

mod4 = lm_robust(prd_change_mayor_15_18~distance_damaged_houses+as.factor(zona.geotec)+urbage+literacy_15+unemployment+seguro_popular+president2018_share_morena+as.factor(name.del), data = master, se_type = "stata")

mod5 = lm_robust(prd_change_mayor_15_18~distance_damaged_houses_highrisk+as.factor(zona.geotec)+urbage+literacy_15+unemployment+seguro_popular+president2018_share_morena+as.factor(name.del), data = master, se_type = "stata")

mod6 = lm_robust(prd_change_mayor_15_18~distance_damaged_multifamily+as.factor(zona.geotec)+urbage+literacy_15+unemployment+seguro_popular+president2018_share_morena+as.factor(name.del), data = master, se_type = "stata")

texreg::texreg(list(mod4, mod5, mod6), digits = 3)

##############################################
### Supplementary Information -- Table S4 ####               
##############################################

#Table S4: Results for models using per capita measures of damage—PRD candidates for mayor

#PRD per capita

mod4b = lm_robust(prd_change_mayor_15_18~damaged_houses_1k+as.factor(zona.geotec)+urbage+literacy_15+unemployment+seguro_popular+president2018_share_morena+as.factor(name.del), data = master, se_type = "stata")

mod5b = lm_robust(prd_change_mayor_15_18~damaged_houses_highrisk_1k+as.factor(zona.geotec)+urbage+literacy_15+unemployment+seguro_popular+president2018_share_morena+as.factor(name.del), data = master, se_type = "stata")

mod6b = lm_robust(prd_change_mayor_15_18~damaged_multifamily_1k+as.factor(zona.geotec)+urbage+literacy_15+unemployment+seguro_popular+president2018_share_morena+as.factor(name.del), data = master, se_type = "stata")

texreg::texreg(list(mod4b, mod5b, mod6b), digits = 3)

##############################################
### Supplementary Information -- Table S5 ####               
##############################################

#Table S5: Results for models using distance-based measures of damage—MORENA candidates for mayor

mod7 = lm_robust(morena_change_mayor_15_18~distance_damaged_houses+as.factor(zona.geotec)+urbage+literacy_15+unemployment+seguro_popular+president2018_share_morena+as.factor(name.del), data = master, se_type = "stata")

mod8 = lm_robust(morena_change_mayor_15_18~distance_damaged_houses_highrisk+as.factor(zona.geotec)+urbage+literacy_15+unemployment+seguro_popular+president2018_share_morena+as.factor(name.del), data = master, se_type = "stata")

mod9 = lm_robust(morena_change_mayor_15_18~distance_damaged_multifamily+as.factor(zona.geotec)+urbage+literacy_15+unemployment+seguro_popular+president2018_share_morena+as.factor(name.del), data = master, se_type = "stata")

texreg::texreg(list(mod7, mod8, mod9), digits = 3)

##############################################
### Supplementary Information -- Table S6 ####               
##############################################

#Table S6: Results for models using per capita measures of damage—MORENA candidates for mayor

#MORENA per capita

mod7b = lm_robust(morena_change_mayor_15_18~damaged_houses_1k+as.factor(zona.geotec)+urbage+literacy_15+unemployment+seguro_popular+president2018_share_morena+as.factor(name.del), data = master, se_type = "stata")

mod8b = lm_robust(morena_change_mayor_15_18~damaged_houses_highrisk_1k+as.factor(zona.geotec)+urbage+literacy_15+unemployment+seguro_popular+president2018_share_morena+as.factor(name.del), data = master, se_type = "stata")

mod9b = lm_robust(morena_change_mayor_15_18~damaged_multifamily_1k+as.factor(zona.geotec)+urbage+literacy_15+unemployment+seguro_popular+president2018_share_morena+as.factor(name.del), data = master, se_type = "stata")

texreg::texreg(list(mod7b, mod8b, mod9b), digits = 3)

##############################################
### Supplementary Information -- Table S7 ####               
##############################################

#Table S7: Results for models using distance-based measures of damage—PRD candidates for deputy

#PRD distance based

mod10 = lm_robust(prd_change_deputies_15_18~distance_damaged_houses+as.factor(zona.geotec)+urbage+literacy_15+unemployment+seguro_popular+president2018_share_morena+as.factor(name.del), data = master, se_type = "stata")

mod11 = lm_robust(prd_change_deputies_15_18~distance_damaged_houses_highrisk+as.factor(zona.geotec)+urbage+literacy_15+unemployment+seguro_popular+president2018_share_morena+as.factor(name.del), data = master, se_type = "stata")

mod12 = lm_robust(prd_change_deputies_15_18~distance_damaged_multifamily+as.factor(zona.geotec)+urbage+literacy_15+unemployment+seguro_popular+president2018_share_morena+as.factor(name.del), data = master, se_type = "stata")

texreg::texreg(list(mod10, mod11, mod12), digits = 3)

##############################################
### Supplementary Information -- Table S8 ####               
##############################################

#Table S8: Results for models using per capita measures of damage—PRD candidates for deputy

#PRD per capita

mod10b = lm_robust(prd_change_deputies_15_18~damaged_houses_1k+as.factor(zona.geotec)+urbage+literacy_15+unemployment+seguro_popular+president2018_share_morena+as.factor(name.del), data = master, se_type = "stata")

mod11b = lm_robust(prd_change_deputies_15_18~damaged_houses_highrisk_1k+as.factor(zona.geotec)+urbage+literacy_15+unemployment+seguro_popular+president2018_share_morena+as.factor(name.del), data = master, se_type = "stata")

mod12b = lm_robust(prd_change_deputies_15_18~damaged_multifamily_1k+as.factor(zona.geotec)+urbage+literacy_15+unemployment+seguro_popular+president2018_share_morena+as.factor(name.del), data = master, se_type = "stata")

texreg::texreg(list(mod10b, mod11b, mod12b), digits = 3)

##############################################
### Supplementary Information -- Table S9 ####               
##############################################

#Table S9: Results for models using distance-based measures of damage—MORENA candidates for deputy.

#MORENA distance based

mod13 =  lm_robust(morena_change_deputies_15_18~distance_damaged_houses+as.factor(zona.geotec)+urbage+literacy_15+unemployment+seguro_popular+president2018_share_morena+as.factor(name.del), data = master, se_type = "stata")

mod14 = lm_robust(morena_change_deputies_15_18~distance_damaged_houses_highrisk+as.factor(zona.geotec)+urbage+literacy_15+unemployment+seguro_popular+president2018_share_morena+as.factor(name.del), data = master, se_type = "stata")

mod15 = lm_robust(morena_change_deputies_15_18~distance_damaged_multifamily+as.factor(zona.geotec)+urbage+literacy_15+unemployment+seguro_popular+president2018_share_morena+as.factor(name.del), data = master, se_type = "stata")

texreg::texreg(list(mod13, mod14, mod15), digits = 3)

###############################################
### Supplementary Information -- Table S10 ####               
###############################################

#Table S10: Results for models using per capita measures of damage—MORENA candidates for deputy.

#MORENA per capita

mod13b = lm_robust(morena_change_deputies_15_18~damaged_houses_1k+as.factor(zona.geotec)+urbage+literacy_15+unemployment+seguro_popular+president2018_share_morena+as.factor(name.del), data = master, se_type = "stata")

mod14b = lm_robust(morena_change_deputies_15_18~damaged_houses_highrisk_1k+as.factor(zona.geotec)+urbage+literacy_15+unemployment+seguro_popular+president2018_share_morena+as.factor(name.del), data = master, se_type = "stata")

mod15b = lm_robust(morena_change_deputies_15_18~damaged_multifamily_1k+as.factor(zona.geotec)+urbage+literacy_15+unemployment+seguro_popular+president2018_share_morena+as.factor(name.del), data = master, se_type = "stata")

texreg::texreg(list(mod13b, mod14b, mod15b), digits = 3)

###############################################
### Supplementary Information -- Table S11 ####              
###############################################

#Table S11: Results for models using per capita measures of disaster aid (reconstruction credits) for incumbent and challenger candidates to different levels of office.

#Reconstruction Credits

mod16 = lm_robust(prd_change_mayor_15_18~housingrelief_1k+damaged_multifamily_1k+as.factor(zona.geotec)+urbage+literacy_15+unemployment+seguro_popular+president2018_share_morena+as.factor(name.del), data = master, se_type = "stata")

mod17 = lm_robust(morena_change_mayor_15_18~housingrelief_1k+damaged_multifamily_1k+as.factor(zona.geotec)+urbage+literacy_15+unemployment+seguro_popular+president2018_share_morena+as.factor(name.del), data = master, se_type = "stata")

mod18 = lm_robust(prd_change_deputies_15_18~housingrelief_1k+damaged_multifamily_1k+as.factor(zona.geotec)+urbage+literacy_15+unemployment+seguro_popular+president2018_share_morena+as.factor(name.del), data = master, se_type = "stata")

mod19 = lm_robust(morena_change_deputies_15_18~housingrelief_1k+damaged_multifamily_1k+as.factor(zona.geotec)+urbage+literacy_15+unemployment+seguro_popular+president2018_share_morena+as.factor(name.del), data = master, se_type = "stata")

mod20 = lm_robust(prd_change_gov_12_18~housingrelief_1k+damaged_multifamily_1k+as.factor(zona.geotec)+urbage+literacy_15+unemployment+seguro_popular+president2018_share_morena+as.factor(name.del), data = master, se_type = "stata")

texreg::texreg(list(mod16, mod17, mod18, mod19, mod20), digits = 5)

###############################################
### Supplementary Information -- Table S12 ####               
###############################################

#Table S12: Results for models using per capita measures of disaster aid (risk reduction policies) for incumbent and challenger candidates to different levels of office.

#Risk Reduction Policies

mod21 = lm_robust(prd_change_mayor_15_18~procuraduriarelief_1k+damaged_multifamily_1k+as.factor(zona.geotec)+urbage+literacy_15+unemployment+seguro_popular+president2018_share_morena+as.factor(name.del), data = master, se_type = "stata")

mod22 = lm_robust(morena_change_mayor_15_18~procuraduriarelief_1k+damaged_multifamily_1k+as.factor(zona.geotec)+urbage+literacy_15+unemployment+seguro_popular+president2018_share_morena+as.factor(name.del), data = master, se_type = "stata")

mod23 = lm_robust(prd_change_deputies_15_18~procuraduriarelief_1k+damaged_multifamily_1k+as.factor(zona.geotec)+urbage+literacy_15+unemployment+seguro_popular+president2018_share_morena+as.factor(name.del), data = master, se_type = "stata")

mod24 = lm_robust(morena_change_deputies_15_18~procuraduriarelief_1k+damaged_multifamily_1k+as.factor(zona.geotec)+urbage+literacy_15+unemployment+seguro_popular+president2018_share_morena+as.factor(name.del), data = master, se_type = "stata")

mod25 = lm_robust(prd_change_gov_12_18~procuraduriarelief_1k+damaged_multifamily_1k+as.factor(zona.geotec)+urbage+literacy_15+unemployment+seguro_popular+president2018_share_morena+as.factor(name.del), data = master, se_type = "stata")

texreg::texreg(list(mod21, mod22, mod23, mod24, mod25), digits = 5)

###############################################
### Supplementary Information -- Table S13 ####               
###############################################

#Table S13: Results for spatial auto-regressive models—PRD candidates for mayor / distance-based measure of damage

cdmxNbList <- mexico_city_map %>% 
  spdep::poly2nb(c('CLAVEGEO')) %>%
  spdep::nb2listw(zero.policy = TRUE)

#City Incumbent

#Models with distance

models1_mayor_sp <- spatialreg::errorsarlm(prd_change_mayor_15_18~distance_damaged_houses+as.factor(zona.geotec)+urbage+literacy_15+unemployment+seguro_popular+president2018_share_morena+as.factor(name.del),
                                           data = mexico_city_map,
                                           listw = cdmxNbList,
                                           zero.policy = TRUE, 
                                           na.action = na.omit)

models2_mayor_sp <- spatialreg::errorsarlm(prd_change_mayor_15_18~distance_damaged_houses_highrisk+as.factor(zona.geotec)+urbage+literacy_15+unemployment+seguro_popular+president2018_share_morena+as.factor(name.del),
                                           data = mexico_city_map,
                                           listw = cdmxNbList,
                                           zero.policy = TRUE, 
                                           na.action = na.omit)

models3_mayor_sp <- spatialreg::errorsarlm(prd_change_mayor_15_18~distance_damaged_multifamily+as.factor(zona.geotec)+urbage+literacy_15+unemployment+seguro_popular+president2018_share_morena+as.factor(name.del),
                                           data = mexico_city_map,
                                           listw = cdmxNbList,
                                           zero.policy = TRUE, 
                                           na.action = na.omit)

texreg::texreg(list(models1_mayor_sp, models2_mayor_sp, models3_mayor_sp), digits = 5)

###############################################
### Supplementary Information -- Table S14 ####               
###############################################

#Table S14: Results for spatial auto-regressive models—PRD candidates for mayor / per capita measure of damage

#Models per capita

models4_mayor_sp <- spatialreg::errorsarlm(prd_change_mayor_15_18~damaged_houses_1k+as.factor(zona.geotec)+urbage+literacy_15+unemployment+seguro_popular+president2018_share_morena+as.factor(name.del),
                                           data = mexico_city_map,
                                           listw = cdmxNbList,
                                           zero.policy = TRUE, 
                                           na.action = na.omit)

models5_mayor_sp <- spatialreg::errorsarlm(prd_change_mayor_15_18~damaged_houses_highrisk_1k+as.factor(zona.geotec)+urbage+literacy_15+unemployment+seguro_popular+president2018_share_morena+as.factor(name.del),
                                           data = mexico_city_map,
                                           listw = cdmxNbList,
                                           zero.policy = TRUE, 
                                           na.action = na.omit)

models6_mayor_sp <- spatialreg::errorsarlm(prd_change_mayor_15_18~damaged_multifamily_1k+as.factor(zona.geotec)+urbage+literacy_15+unemployment+seguro_popular+president2018_share_morena+as.factor(name.del),
                                           data = mexico_city_map,
                                           listw = cdmxNbList,
                                           zero.policy = TRUE, 
                                           na.action = na.omit)

texreg::texreg(list(models4_mayor_sp, models5_mayor_sp, models6_mayor_sp), digits = 5)

###############################################
### Supplementary Information -- Table S15 ####               
###############################################

#Table S15: Results for spatial auto-regressive models—MORENA candidates for mayor / distance-based measure of damage

#City Challenger

#Models with distance

models7_mayor_sp = spatialreg::errorsarlm(morena_change_mayor_15_18~distance_damaged_houses+as.factor(zona.geotec)+urbage+literacy_15+unemployment+seguro_popular+president2018_share_morena+as.factor(name.del),
                                          data = mexico_city_map,
                                          listw = cdmxNbList,
                                          zero.policy = TRUE, 
                                          na.action = na.omit)

models8_mayor_sp = spatialreg::errorsarlm(morena_change_mayor_15_18~distance_damaged_houses_highrisk+as.factor(zona.geotec)+urbage+literacy_15+unemployment+seguro_popular+president2018_share_morena+as.factor(name.del),
                                          data = mexico_city_map,
                                          listw = cdmxNbList,
                                          zero.policy = TRUE, 
                                          na.action = na.omit)

models9_mayor_sp = spatialreg::errorsarlm(morena_change_mayor_15_18~distance_damaged_multifamily+as.factor(zona.geotec)+urbage+literacy_15+unemployment+seguro_popular+president2018_share_morena+as.factor(name.del),
                                          data = mexico_city_map,
                                          listw = cdmxNbList,
                                          zero.policy = TRUE, 
                                          na.action = na.omit)

texreg::texreg(list(models7_mayor_sp, models8_mayor_sp, models9_mayor_sp), digits = 5)

###############################################
### Supplementary Information -- Table S16 ####               
###############################################

#Table S16: Results for spatial auto-regressive models—MORENA candidates for mayor / per capita measure of damage

#Models per capita

models10_mayor_sp = spatialreg::errorsarlm(morena_change_mayor_15_18~damaged_houses_1k+as.factor(zona.geotec)+urbage+literacy_15+unemployment+seguro_popular+president2018_share_morena+as.factor(name.del),
                                           data = mexico_city_map,
                                           listw = cdmxNbList,
                                           zero.policy = TRUE, 
                                           na.action = na.omit)

models11_mayor_sp = spatialreg::errorsarlm(morena_change_mayor_15_18~damaged_houses_highrisk_1k+as.factor(zona.geotec)+urbage+literacy_15+unemployment+seguro_popular+president2018_share_morena+as.factor(name.del),
                                           data = mexico_city_map,
                                           listw = cdmxNbList,
                                           zero.policy = TRUE, 
                                           na.action = na.omit)

models12_mayor_sp = spatialreg::errorsarlm(morena_change_mayor_15_18~damaged_multifamily_1k+as.factor(zona.geotec)+urbage+literacy_15+unemployment+seguro_popular+president2018_share_morena+as.factor(name.del),
                                           data = mexico_city_map,
                                           listw = cdmxNbList,
                                           zero.policy = TRUE, 
                                           na.action = na.omit)

texreg::texreg(list(models10_mayor_sp, models11_mayor_sp, models12_mayor_sp), digits = 5)

###############################################
### Supplementary Information -- Table S17 ####               
###############################################

#Table S17: Results for spatial auto-regressive models—PRD candidates for deputy / distance-based measure of damage

#City Incumbent

#Models with distance

models1_deputies_sp = spatialreg::errorsarlm(prd_change_deputies_15_18~distance_damaged_houses+as.factor(zona.geotec)+urbage+literacy_15+unemployment+seguro_popular+president2018_share_morena+as.factor(name.del),
                                             data = mexico_city_map,
                                             listw = cdmxNbList,
                                             zero.policy = TRUE, 
                                             na.action = na.omit)

models2_deputies_sp = spatialreg::errorsarlm(prd_change_deputies_15_18~distance_damaged_houses_highrisk+as.factor(zona.geotec)+urbage+literacy_15+unemployment+seguro_popular+president2018_share_morena+as.factor(name.del),
                                             data = mexico_city_map,
                                             listw = cdmxNbList,
                                             zero.policy = TRUE, 
                                             na.action = na.omit)

models3_deputies_sp = spatialreg::errorsarlm(prd_change_deputies_15_18~distance_damaged_multifamily+as.factor(zona.geotec)+urbage+literacy_15+unemployment+seguro_popular+president2018_share_morena+as.factor(name.del),
                                             data = mexico_city_map,
                                             listw = cdmxNbList,
                                             zero.policy = TRUE, 
                                             na.action = na.omit)

texreg::texreg(list(models1_deputies_sp, models2_deputies_sp, models3_deputies_sp), digits = 5)


###############################################
### Supplementary Information -- Table S18 ####               
###############################################

#Table S18: Results for spatial auto-regressive models—PRD candidates for deputy / per capita measure of damage

#Models per capita

models4_deputies_sp = spatialreg::errorsarlm(prd_change_deputies_15_18~damaged_houses_1k+as.factor(zona.geotec)+urbage+literacy_15+unemployment+seguro_popular+president2018_share_morena+as.factor(name.del),
                                             data = mexico_city_map,
                                             listw = cdmxNbList,
                                             zero.policy = TRUE, 
                                             na.action = na.omit)

models5_deputies_sp = spatialreg::errorsarlm(prd_change_deputies_15_18~damaged_houses_highrisk_1k+as.factor(zona.geotec)+urbage+literacy_15+unemployment+seguro_popular+president2018_share_morena+as.factor(name.del),
                                             data = mexico_city_map,
                                             listw = cdmxNbList,
                                             zero.policy = TRUE, 
                                             na.action = na.omit)

models6_deputies_sp = spatialreg::errorsarlm(prd_change_deputies_15_18~damaged_multifamily_1k+as.factor(zona.geotec)+urbage+literacy_15+unemployment+seguro_popular+president2018_share_morena+as.factor(name.del),
                                             data = mexico_city_map,
                                             listw = cdmxNbList,
                                             zero.policy = TRUE, 
                                             na.action = na.omit)

texreg::texreg(list(models4_deputies_sp, models5_deputies_sp, models6_deputies_sp), digits = 5)

###############################################
### Supplementary Information -- Table S19 ####               
###############################################

#Table S19: Results for spatial auto-regressive models—MORENA candidates for deputy / distance-based measure of damage

#City Challenger

#Models with distance

models7_deputies_sp = spatialreg::errorsarlm(morena_change_deputies_15_18~distance_damaged_houses+as.factor(zona.geotec)+urbage+literacy_15+unemployment+seguro_popular+president2018_share_morena+as.factor(name.del),
                                             data = mexico_city_map,
                                             listw = cdmxNbList,
                                             zero.policy = TRUE, 
                                             na.action = na.omit)

models8_deputies_sp = spatialreg::errorsarlm(morena_change_deputies_15_18~distance_damaged_houses_highrisk+as.factor(zona.geotec)+urbage+literacy_15+unemployment+seguro_popular+president2018_share_morena+as.factor(name.del),
                                             data = mexico_city_map,
                                             listw = cdmxNbList,
                                             zero.policy = TRUE, 
                                             na.action = na.omit)

models9_deputies_sp = spatialreg::errorsarlm(morena_change_deputies_15_18~distance_damaged_multifamily+as.factor(zona.geotec)+urbage+literacy_15+unemployment+seguro_popular+president2018_share_morena+as.factor(name.del),
                                             data = mexico_city_map,
                                             listw = cdmxNbList,
                                             zero.policy = TRUE, 
                                             na.action = na.omit)

texreg::texreg(list(models7_deputies_sp, models8_deputies_sp, models9_deputies_sp), digits = 5)

###############################################
### Supplementary Information -- Table S20 ####               
###############################################

#Table S20: Results for spatial auto-regressive models—MORENA candidates for deputy / per capita measure of damage

#Models per capita

models10_deputies_sp = spatialreg::errorsarlm(morena_change_deputies_15_18~damaged_houses_1k+as.factor(zona.geotec)+urbage+literacy_15+unemployment+seguro_popular+president2018_share_morena+as.factor(name.del),
                                              data = mexico_city_map,
                                              listw = cdmxNbList,
                                              zero.policy = TRUE, 
                                              na.action = na.omit)

models11_deputies_sp = spatialreg::errorsarlm(morena_change_deputies_15_18~damaged_houses_highrisk_1k+as.factor(zona.geotec)+urbage+literacy_15+unemployment+seguro_popular+president2018_share_morena+as.factor(name.del),
                                              data = mexico_city_map,
                                              listw = cdmxNbList,
                                              zero.policy = TRUE, 
                                              na.action = na.omit)

models12_deputies_sp = spatialreg::errorsarlm(morena_change_deputies_15_18~damaged_multifamily_1k+as.factor(zona.geotec)+urbage+literacy_15+unemployment+seguro_popular+president2018_share_morena+as.factor(name.del),
                                              data = mexico_city_map,
                                              listw = cdmxNbList,
                                              zero.policy = TRUE, 
                                              na.action = na.omit)

texreg::texreg(list(models10_deputies_sp, models11_deputies_sp, models12_deputies_sp), digits = 5)

###############################################
### Supplementary Information -- Table S21 ####               
###############################################

#Table S21: PRD Candidate for Governor. Treatment is defined as having more than one: (1) damaged
#housing unit, (2) severely-damaged housing unit, and (3) damaged multi-family unit per 1000. The outcome
#is the change in the vote from 2012 to 2018.

#Balance: Propensity Score Matching (for all the following Tables)

matching_cov <- c('water', 'seguro_popular', 'literacy_15', 'unemployment')
master_nomiss <- master %>%  # MatchIt does not allow missing values
  select(prd_change_mayor_15_18, morena_change_mayor_15_18, prd_change_gov_12_18,
         prd_change_deputies_15_18, morena_change_deputies_15_18, housingrelief_1k,
         treatment1, treatment2, treatment3, zona.geotec, urbage, 
         procuraduriarelief_1k, one_of(matching_cov)) %>%
  na.omit()

mod_match1_nearest <- matchit(treatment1 ~ zona.geotec+urbage+water+seguro_popular+literacy_15+unemployment,
                              method = "nearest", data = master_nomiss)
mod_match2_nearest <- matchit(treatment2 ~ zona.geotec+urbage+water+seguro_popular+literacy_15+unemployment,
                              method = "nearest", data = master_nomiss)
mod_match3_nearest <- matchit(treatment3 ~ zona.geotec+urbage+water+seguro_popular+literacy_15+unemployment,
                              method = "nearest", data = master_nomiss)

mod_match1_opt <- matchit(treatment1 ~ zona.geotec+urbage+water+seguro_popular+literacy_15+unemployment,
                          method = "optimal", data = master_nomiss)
mod_match2_opt <- matchit(treatment2 ~ zona.geotec+urbage+water+seguro_popular+literacy_15+unemployment,
                          method = "optimal", data = master_nomiss)
mod_match3_opt <- matchit(treatment3 ~ zona.geotec+urbage+water+seguro_popular+literacy_15+unemployment,
                          method = "optimal", data = master_nomiss)

dta_m1_opt <- match.data(mod_match1_opt)
dta_m2_opt <- match.data(mod_match2_opt)
dta_m3_opt <- match.data(mod_match3_opt)

dta_m1_near <- match.data(mod_match1_nearest)
dta_m2_near <- match.data(mod_match2_nearest)
dta_m3_near <- match.data(mod_match3_nearest)

#PRD Governor
lm_match_gov1 <- lm(prd_change_gov_12_18 ~ treatment1, data = dta_m1_near)
lm_match_gov2 <- lm(prd_change_gov_12_18 ~ treatment2, data = dta_m2_near)
lm_match_gov3 <- lm(prd_change_gov_12_18 ~ treatment3, data = dta_m3_near)

lm_match_gov4 <- lm(prd_change_gov_12_18 ~ treatment1, data = dta_m1_opt)
lm_match_gov5 <- lm(prd_change_gov_12_18 ~ treatment2, data = dta_m2_opt)
lm_match_gov6 <- lm(prd_change_gov_12_18 ~ treatment3, data = dta_m3_opt)
texreg(list(lm_match_gov1, lm_match_gov4, lm_match_gov2, lm_match_gov5, lm_match_gov3, lm_match_gov6), digits = 4)

###############################################
### Supplementary Information -- Table S22 ####               
###############################################

#Table S22: PRD Candidates for Mayor. Treatment is defined as having more than one: (1) damaged
#housing unit, (2) severely-damaged housing unit, and (3) damaged multi-family unit per 1000. The outcome
#is the change in the vote from 2015 to 2018.

#PRD Mayor

lm_match_may1 <- lm(prd_change_mayor_15_18 ~ treatment1, data = dta_m1_near)
lm_match_may2 <- lm(prd_change_mayor_15_18 ~ treatment2, data = dta_m2_near)
lm_match_may3 <- lm(prd_change_mayor_15_18 ~ treatment3, data = dta_m3_near)

lm_match_may4 <- lm(prd_change_mayor_15_18 ~ treatment1, data = dta_m1_opt)
lm_match_may5 <- lm(prd_change_mayor_15_18 ~ treatment2, data = dta_m2_opt)
lm_match_may6 <- lm(prd_change_mayor_15_18 ~ treatment3, data = dta_m3_opt)
texreg(list(lm_match_may1, lm_match_may4, lm_match_may2, lm_match_may5, lm_match_may3, lm_match_may6), digits = 4)

###############################################
### Supplementary Information -- Table S23 ####               
###############################################

#Table S23: PRD Candidates for Deputy. Treatment is defined as having more than one: (1) damaged
#housing unit, (2) severely-damaged housing unit, and (3) damaged multi-family unit per 1000. The outcome
#is the change in the vote from 2015 to 2018.

#PRD Deputy

lm_match_dep1 <- lm(prd_change_deputies_15_18 ~ treatment1, data = dta_m1_near)
lm_match_dep2 <- lm(prd_change_deputies_15_18 ~ treatment2, data = dta_m2_near)
lm_match_dep3 <- lm(prd_change_deputies_15_18 ~ treatment3, data = dta_m3_near)

lm_match_dep4 <- lm(prd_change_deputies_15_18 ~ treatment1, data = dta_m1_opt)
lm_match_dep5 <- lm(prd_change_deputies_15_18 ~ treatment2, data = dta_m2_opt)
lm_match_dep6 <- lm(prd_change_deputies_15_18 ~ treatment3, data = dta_m3_opt)
texreg(list(lm_match_dep1, lm_match_dep4, lm_match_dep2, lm_match_dep5, lm_match_dep3, lm_match_dep6), digits = 4)

###############################################
### Supplementary Information -- Table S24 ####               
###############################################

#Table S24: MORENA Candidates for Mayor. Treatment is defined as having more than one: (1)
#damaged housing unit, (2) severely-damaged housing unit, and (3) damaged multi-family unit per 1000.
#The outcome is the change in the vote from 2015 to 2018.

#MORENA Mayor

lm_match_may1 <- lm(morena_change_mayor_15_18 ~ treatment1, data = dta_m1_near)
lm_match_may2 <- lm(morena_change_mayor_15_18 ~ treatment2, data = dta_m2_near)
lm_match_may3 <- lm(morena_change_mayor_15_18 ~ treatment3, data = dta_m3_near)

lm_match_may4 <- lm(morena_change_mayor_15_18 ~ treatment1, data = dta_m1_opt)
lm_match_may5 <- lm(morena_change_mayor_15_18 ~ treatment2, data = dta_m2_opt)
lm_match_may6 <- lm(morena_change_mayor_15_18 ~ treatment3, data = dta_m3_opt)
texreg(list(lm_match_may1, lm_match_may4, lm_match_may2, lm_match_may5, lm_match_may3, lm_match_may6), digits = 4)

###############################################
### Supplementary Information -- Table S25 ####               
###############################################

#Table S25: MORENA Candidates for Deputy. Treatment is defined as having more than one: (1)
#damaged housing unit, (2) severely-damaged housing unit, and (3) damaged multi-family unit per 1000.
#The outcome is the change in the vote from 2015 to 2018.

#MORENA Deputy

lm_match_dep1 <- lm(morena_change_deputies_15_18 ~ treatment1, data = dta_m1_near)
lm_match_dep2 <- lm(morena_change_deputies_15_18 ~ treatment2, data = dta_m2_near)
lm_match_dep3 <- lm(morena_change_deputies_15_18 ~ treatment3, data = dta_m3_near)

lm_match_dep4 <- lm(morena_change_deputies_15_18 ~ treatment1, data = dta_m1_opt)
lm_match_dep5 <- lm(morena_change_deputies_15_18 ~ treatment2, data = dta_m2_opt)
lm_match_dep6 <- lm(morena_change_deputies_15_18 ~ treatment3, data = dta_m3_opt)
texreg(list(lm_match_dep1, lm_match_dep4, lm_match_dep2, lm_match_dep5, lm_match_dep3, lm_match_dep6), digits = 4)

###############################################
### Supplementary Information -- Figure S7 ####               
###############################################

#Figure S7: Figure S7: Comparison of covariate balance in matched sample (black dot) and unmatched
#sample (white dot). Method employed: nearest neighbor matching. Treatment: presence of any damaged
#housing unit after the 19-S earthquake. Vertical lines indicate standardized mean differences of + / - 0.05.

plot(summary(mod_match1_nearest, interactions = FALSE),
     var.order = "alphabetical", position = NULL, threshold = c(.05, .05), abs = F)

###############################################
### Supplementary Information -- Figure S8 ####               
###############################################

#Figure S8: Comparison of covariate balance in matched sample (black dot) and unmatched
#sample (white dot). Method employed: optimal matching. Treatment: presence of any damaged housing
#unit after the 19-S earthquake. Vertical lines indicate standardized mean differences of + / - 0.05.

plot(summary(mod_match1_opt, interactions = FALSE),
     var.order = "alphabetical", position = NULL, threshold = c(.05, .05), abs = F)

###############################################
### Supplementary Information -- Figure S9 ####               
###############################################

#Figure S9: Comparison of covariate balance in matched sample (black dot) and unmatched
#sample (white dot). Method employed: nearest neighbor matching. 
#Treatment: presence of severelydamaged housing units after the 19-S earthquake. Vertical lines indicate standardized mean differences of +  / - 0.05.

plot(summary(mod_match2_nearest, interactions = FALSE),
     var.order = "alphabetical", position = NULL, threshold = c(.05, .05), abs = F)

################################################
### Supplementary Information -- Figure S10 ####               
################################################

#Figure S10: Comparison of covariate balance in matched sample (black dot) and unmatched
#sample (white dot). Method employed: optimal matching. Treatment: presence of severely-damaged
#housing units after the 19-S earthquake. Vertical lines indicate standardized mean differences of + / - 0.05.

plot(summary(mod_match2_opt, interactions = FALSE),
     var.order = "alphabetical", position = NULL, threshold = c(.05, .05), abs = F)

################################################
### Supplementary Information -- Figure S11 ####               
################################################

#Figure S11: Comparison of covariate balance in matched sample (black dot) and unmatched
#sample (white dot). Method employed: nearest neighbor matching. Treatment: presence of damaged
#multi-family units after the 19-S earthquake. Vertical lines indicate standardized mean differences of + / -  0.05.

plot(summary(mod_match3_nearest, interactions = FALSE),
     var.order = "alphabetical", position = NULL, threshold = c(.05, .05), abs = F)

################################################
### Supplementary Information -- Figure S12 ####               
################################################

#Figure S12: Comparison of covariate balance in matched sample (black dot) and unmatched
#sample (white dot). Method employed: optimal matching. Treatment: presence of damaged multi-family
#units after the 19-S earthquake. Vertical lines indicate standardized mean differences of + / - 0.05.

plot(summary(mod_match3_opt, interactions = FALSE),
     var.order = "alphabetical", position = NULL, threshold = c(.05, .05), abs = F)